package com.wondertek.poms.sync.manager.main;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.core.utils.OutHttpUtil;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.SyncTask;
import com.wondertek.poms.sync.manager.copyright.CopyrightManager;

import lombok.extern.slf4j.Slf4j;

/**
 * <p><b>MainAssertManager Description:</b> 其他类型媒资业务类</p>
 * @author douzi
 * <b>DATE</b> 2019年8月20日 下午5:28:39
 */
@Slf4j
@Component
public class SpecialMainAssertManager extends ParentManager {
    @Autowired
    IPrdInfoService prdInfoService;
    @Autowired
    IPrdContService prdContService;
    @Autowired
    CopyrightManager copyrightManager;
    @Autowired
    OutHttpUtil outHttpUtil;

    /**
     * <p><b>Title:</b> mainProcess</p>
     * <p><b>Description:</b> 无二级产品主业务流程</p>
     *
     * @param vo
     * @author douzi
     */
    public void mainProcess(Content c, SyncTask st, List<List<PrdInfo>> special) {
        switch (c.getStatus().intValue()) {
            case Constants.ASSET_STATUS_0:
            case Constants.ASSET_STATUS_1:
            case Constants.ASSET_STATUS_3:
                mainUpdateProcess(c, st, special);
                break;
            case Constants.ASSET_STATUS_2:
                mainDelProcess(st, "special-main");
                break;
            default:
                log.info("父子包主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() +  "|不属于任何一种媒资状态:" + c.getStatus());
                break;
        }
    }

    /**
     * <p><b>Title:</b> mainUpdateProcess</p>
     * <p><b>Description:</b> 主流程之更新流程</p>
     * @author douzi
     * @param c
     * @param st
     * @param normalList
     */
    private void mainUpdateProcess(Content c, SyncTask st, List<List<PrdInfo>> special) {
    	for(List<PrdInfo> piList : special) {
			if(piList != null && piList.size()>0) {
				exec(c, st, piList);
			}
    	}
	}

	/**
	 * <p><b>Title:</b> exec</p>
	 * <p><b>Description:</b> 执行一套父子包</p>
	 * @author douzi
	 * @param c
	 * @param st
	 * @param prdInfos
	 */
	private void exec(Content c, SyncTask st, List<PrdInfo> prdInfos) {
		Long newPrdInfoId = getWhichPrdInfo(c, prdInfos);
		
		// 先获取父子包中对象
		List<PrdCont> prdContDbs = new ArrayList<>();
		for (int i = prdInfos.size() - 1; i >= 0; i --) {
			PrdInfo pi = prdInfos.get(i);
			List<PrdCont> pcDbs = prdContService.getPrdContByMmsIdInPrdInfo(c.getAssetId(), pi.getPrdInfoId());
			prdContDbs.addAll(pcDbs);
		}
		log.info("父子包主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() +  "|无二级产品的商品包|父子包中数据库中媒资数量:" + prdContDbs.size() + "|版权id:" + c.getCopyrightObjectId());
		
		if (newPrdInfoId == null) {
    		offineContents(prdContDbs, prdInfos, c, "父子包");
            return;
        }
		
		PrdInfo prdInfo = getPrdInfo(prdInfos, newPrdInfoId);
		
		if (prdContDbs.size() <= 0) {
            inPrdInfo(prdInfo, c, null);
        } else if (prdContDbs.size() == 1) {
        	PrdCont pcDb = prdContDbs.get(0);
        	// 比较数据库中的内容是否和传入内容相同，不同需要走版权pk业务
        	if (pcDb.getContentId().equals(c.getContentId())) {
                inPrdInfo(prdInfo, c, pcDb);
        	} else {
        		findOptimalPrdCont(c, prdInfo, pcDb, "父子包同媒资新内容胜出下线数据库节目");
        	}
        }

        //如果是壳，需要让子集跟着一起去新包
        subSetToNewPrdInfo(newPrdInfoId, prdContDbs, c);
		// 清除其他的包中内容
	    delOtherPrdInfo(c, prdInfo, prdInfos);
	}
}
